package cn.iocoder.yudao.module.shop.service.crosspriceitem;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.crosspriceitem.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.crosspriceitem.ShopCrossPriceItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.crosspriceitem.ShopCrossPriceItemConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.crosspriceitem.ShopCrossPriceItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 跨店铺项目对应 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopCrossPriceItemServiceImpl implements ShopCrossPriceItemService {

    @Resource
    private ShopCrossPriceItemMapper crossPriceItemMapper;

    @Override
    public Long createCrossPriceItem(ShopCrossPriceItemCreateReqVO createReqVO) {
        // 插入
        ShopCrossPriceItemDO crossPriceItem = ShopCrossPriceItemConvert.INSTANCE.convert(createReqVO);
        crossPriceItemMapper.insert(crossPriceItem);
        // 返回
        return crossPriceItem.getId();
    }

    @Override
    public void updateCrossPriceItem(ShopCrossPriceItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateCrossPriceItemExists(updateReqVO.getId());
        // 更新
        ShopCrossPriceItemDO updateObj = ShopCrossPriceItemConvert.INSTANCE.convert(updateReqVO);
        crossPriceItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteCrossPriceItem(Long id) {
        // 校验存在
        validateCrossPriceItemExists(id);
        // 删除
        crossPriceItemMapper.deleteById(id);
    }

    private void validateCrossPriceItemExists(Long id) {
        if (crossPriceItemMapper.selectById(id) == null) {
            throw exception(CROSS_PRICE_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public ShopCrossPriceItemDO getCrossPriceItem(Long id) {
        return crossPriceItemMapper.selectById(id);
    }

    @Override
    public List<ShopCrossPriceItemDO> getCrossPriceItemList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return crossPriceItemMapper.selectBatchIds(ids);
    }
    
    @Override
    public List<ShopCrossPriceItemDO> getByTenantId(Long tenantId){
        return crossPriceItemMapper.selectList(ShopCrossPriceItemDO::getTenantId, tenantId);
    }

    @Override
    public PageResult<ShopCrossPriceItemDO> getCrossPriceItemPage(ShopCrossPriceItemPageReqVO pageReqVO) {
        return crossPriceItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopCrossPriceItemDO> getCrossPriceItemList(ShopCrossPriceItemExportReqVO exportReqVO) {
        return crossPriceItemMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean updateOrCreateBatch(ShopCrossPriceItemBatchCreateReqVO reqVO){
        if(reqVO.getSourceItemId().size() != reqVO.getTargetItemId().size()){
            throw exception(CROSS_PRICE_ITEM_PARAM_ERROR);
        }

        if(reqVO.getTargetItemId().size() != reqVO.getTargetItemId().size()){
            throw exception(CROSS_PRICE_ITEM_PARAM_ERROR);
        }

        List<ShopCrossPriceItemDO> list = new ArrayList<>();
        for(int i = 0; i<reqVO.getSourceItemId().size(); i++){
            ShopCrossPriceItemDO item = getByTenantId(reqVO.getTenantId(), reqVO.getSourceItemId().get(i), reqVO.getTargetTenantId().get(i));

            if(item == null){
                item = new ShopCrossPriceItemDO();
            }

            item.setSourceItemId(reqVO.getSourceItemId().get(i));
            item.setTargetItemId(reqVO.getTargetItemId().get(i));
            item.setTargetTenantId(reqVO.getTargetTenantId().get(i));

            item.setTenantId(reqVO.getTenantId());
            list.add(item);
        }

        crossPriceItemMapper.saveOrUpdateBatch(list);

        return true;
    }

    public ShopCrossPriceItemDO getByTenantId(Long tenantId, Long sourceItemId, Long targetTenantId){
        return crossPriceItemMapper.selectByTenantId(tenantId, sourceItemId, targetTenantId);
    }

}
